function [wMval] = compareCurvatures(procdata1,idx1,procdata2,idx2)

dat1 = procdata1(idx1);
dat2 = procdata2(idx2);

clear procdata1; clear procdata2;

kmat=[1 1; 1 2; 2 1; 2 2];

use_optimization = true;

m = zeros(4,1);

for k = 1:size(kmat, 1)
    tic
    v1 = dat1.U(:, kmat(k, 1));
    v2 = dat2.U(:, kmat(k, 2));
    
    c0 = [0 1 1];
    lb = [-0.1 0.75 1];
    ub = [0.1 1.33 1];
    options = optimoptions('particleswarm','SwarmSize',40,'UseParallel',true);
    %options = optimoptions('particleswarm','SwarmSize',100);
    if use_optimization
        cn = particleswarm(@(c) opt_dist(v1,-v2,c(1:2)), 2, lb(1:2), ub(1:2),options);
        cp = particleswarm(@(c) opt_dist(v1,v2,c(1:2)), 2, lb(1:2), ub(1:2),options);
    else
        cn = c0;
        cp = c0;
    end
    
    if opt_dist(v1, -v2, cn) < opt_dist(v1, v2, cp)
        copt{k}(:) = cn;
        nflag = -1;
        cO = cn;
    else
        copt{k}(:) = cp;
        cO = cp;
        nflag = 1;
    end
    
    
    m(k) = opt_dist(v1, v2*nflag, cO);
    
    toc
end

S(:,1) = dat1.S(1:2);
S(:,2) = dat2.S(1:2);
S(2,:) = S(2,:) - S(1,:);

if m(1) + m(4) > m(2) + m(3)
    flipmat = -1;
    m1= m(2);
    m2 = m(3);
    S1 = S(1,kmat(2,1)) + S(2,kmat(2,2));
    S2 = S(2,kmat(3,1)) + S(2,kmat(3,2));
else
    flipmat = 1;
    m1 = m(1);
    m2 = m(4);
    S1 = S(1,kmat(1,1)) + S(2,kmat(1,2));
    S2 = S(2,kmat(4,1)) + S(2,kmat(4,2));
end
    
    wMval = (S1*m1 + S2*m2)/(S1+S2);

end
